import pyautogui  
import time
import pyperclip  
import re
import csv
import os
from screenshot import screenshot_mesh_viewer,screenshot_texture_viewer
# 点击
RenderDoc_position=(418,883)
wait_time=3.5
def get_mesh_by_mesh_text(mesh_text):
    mesh_pattern = re.compile(r"Element ([^\n]*) 2 0 ")  
    mesh_match=mesh_pattern.match(mesh_text)
    if mesh_match:
        return mesh_match.group(1)
    return ""  
# def get_shader_by_shader_text(shader_text):
#     print(shader_text)
#     shader_pattern = re.compile(r"glCreateProgram([^\n]*)")  
#     shader_match=shader_pattern.match(shader_text)
#     if shader_match:
#         return shader_match.group(1)
#     return ""  
def click(x,y,duration=0.1):
    pyautogui.moveTo(x, y, duration)
    pyautogui.click()
# 按下鼠标并移动到特定坐标
def move(x,y,new_x,new_y,duration=0.1):
    pyautogui.moveTo(x, y,duration)
    pyautogui.mouseDown(x, y, button='left')
    pyautogui.moveTo(new_x, new_y,duration)
    pyautogui.mouseUp()
def screenshot_mesh(name):
    click(702,59)
    time.sleep(wait_time)
    screenshot_mesh_viewer(f"{name}__主视图")
    move(1047,574,693,574)
    time.sleep(1)
    screenshot_mesh_viewer(f"{name}__左视图")
    move(1047,447,1047,767)
    time.sleep(1)
    screenshot_mesh_viewer(f"{name}__俯视图")
def screenshot_texture(name):
    click(828,58)
    time.sleep(wait_time)
    screenshot_texture_viewer(f"{name}__场景图")
def get_mesh_and_shader():
    mesh_and_shader_dict={}
    click(968,59)
    time.sleep(wait_time)
    click(697,138)
    click(675,578)
    # 按下Ctrl+C（复制）  
    pyautogui.hotkey('ctrl', 'c')  
    # 按下Ctrl+C（复制）  
    pyautogui.hotkey('ctrl', 'c')  
    # 获取剪切板内容  
    mesh_text = get_mesh_by_mesh_text(pyperclip.paste())
    mesh_and_shader_dict['mesh']=mesh_text
    pyperclip.copy('')
    click(781,131)
    click(768,197)
    time.sleep(wait_time)
    click(715,528)
    # 按下Ctrl+C（复制）  
    pyautogui.hotkey('ctrl', 'c')  
    # 按下Ctrl+C（复制）  
    pyautogui.hotkey('ctrl', 'c')  
    # 获取剪切板内容  
    shader_text =pyperclip.paste()
    mesh_and_shader_dict['shader']=shader_text
    pyperclip.copy('')
    return mesh_and_shader_dict
def goto_RenderDoc():
    click(RenderDoc_position[0],RenderDoc_position[1])
def sole_element(index,eid):
    pyperclip.copy(eid)
    click(561,99)
    # 按下Ctrl+V（粘贴）  
    pyautogui.hotkey('ctrl', 'v')  
    time.sleep(wait_time)
    pyperclip.copy("")
    screenshot_mesh(f"{index}_{eid}")
    screenshot_texture(f"{index}_{eid}")
    return get_mesh_and_shader()
# 获取eid.txt的eid列表
with open("eid.txt", 'r', encoding='utf-8') as file:  # 'r'表示以只读模式打开文件，'utf-8'是常见的编码方式  
    eids = file.readlines()  

os.system("mkdir 截图")
goto_RenderDoc()
element_dict={}
# 循环列表
index=1
for eid in eids:  
    # 去除每行末尾的换行符（可选）  
    eid = eid.rstrip('\n')  
    if eid=='':
         continue
    now_element_dict=sole_element(index,eid)
    element_dict[eid]=now_element_dict
    index+=1
with open("统计结果.csv", 'w', newline='', encoding='utf-8') as file:
            writer = csv.writer(file)
            writer.writerow(['Index','EID','Mesh','Shader'])
            index=1
            for item in element_dict:
                writer.writerow([
                            index,
                            item,
                            element_dict[item]['mesh'],
                            element_dict[item]['shader']])
                index+=1
click(1437,878)